package com.neusoft.neuiotms.dcom.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.InspectionRuleProjectItemConverter;
import com.neusoft.neuiotms.dcom.dto.InspectionRuleProjectItemDTO;
import com.neusoft.neuiotms.dcom.model.InspectionRuleProjectItem;
import com.neusoft.neuiotms.dcom.repository.InspectionRuleProjectItemRepository;
import com.neusoft.neuiotms.dcom.service.InspectionRuleProjectItemService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class InspectionRuleProjectItemServiceImpl implements InspectionRuleProjectItemService {

    @Autowired
    private InspectionRuleProjectItemRepository itemRepository;

    @Autowired
    private InspectionRuleProjectItemConverter itemConverter;

    @Override
    public List<InspectionRuleProjectItemDTO> index(final Searchable searchable) {
        final List<InspectionRuleProjectItem> models = this.itemRepository.index(searchable);
        return this.itemConverter.toListDTO(models);
    }

    @Override
    public Page<InspectionRuleProjectItemDTO> page(final Searchable searchable, final Pageable pageable) {
        final Page<InspectionRuleProjectItem> models = this.itemRepository.page(searchable, pageable);
        return this.itemConverter.toPageDTO(models);
    }

    @Override
    public InspectionRuleProjectItemDTO create(final InspectionRuleProjectItemDTO dto) {

        //        final InspectionRuleProjectItem check = this.itemRepository.findByName(dto.getName());
        //        if (check != null) {
        //            InspectionRuleProjectItemServiceImpl.log.info(ExceptionConstants.NAME_CAN_NOT_DUPLICATE);
        //            throw new UnitedException(ExceptionConstants.NAME_CAN_NOT_DUPLICATE, new Object[] {});
        //        }

        final InspectionRuleProjectItem model = this.itemConverter.toModel(dto);
        this.itemRepository.save(model);
        return this.itemConverter.toDTO(model);
    }

    @Override
    public InspectionRuleProjectItemDTO update(final Long id, final InspectionRuleProjectItemDTO dto) {
        InspectionRuleProjectItem model;
        try {
            model = this.itemRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            InspectionRuleProjectItemServiceImpl.log.error(e.getMessage(), e);
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        //        final InspectionRuleProjectItem check = this.itemRepository.findByName(dto.getName());
        //        if ((check != null) && (check.getId().longValue() != id.longValue())) {
        //            InspectionRuleProjectItemServiceImpl.log.info(ExceptionConstants.NAME_CAN_NOT_DUPLICATE);
        //            throw new UnitedException(ExceptionConstants.NAME_CAN_NOT_DUPLICATE, new Object[] {id });
        //        }

        model = this.itemConverter.toModel(dto);
        this.itemRepository.save(model);
        return this.itemConverter.toDTO(model);
    }

    @Override
    public InspectionRuleProjectItemDTO detail(final Long id) {
        InspectionRuleProjectItem model;
        try {
            model = this.itemRepository.getOne(id);
            return this.itemConverter.toDTO(model);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
    }

    @Override
    public void delete(final Long id) {
        try {
            this.itemRepository.deleteById(id);
        } catch (final Exception e) {
            throw new UnitedException("已经绑定巡检规则或者巡检任务，暂不允许删除", new Object[] {id });
        }
    }

    @Override
    @Transactional
    public void deleteBatch(final Long[] ids) {
        this.itemRepository.deleteByIdIn(new ArrayList<>(Arrays.asList(ids)));

    }

}
